package com.chinasofti1.info_manager.dao;

import com.chinasofti1.info_manager.domain.Student;

public class StudentDao implements BaseStudentDao{
     //创建学生数组对象
    private  static Student[] stus=new Student[5];
    static {
        Student stu1 = new Student("0011","lilei","13","2010-10-10");
        Student stu2 = new Student("0011","zhangsan","12","2010-10-10");
        stus[0]=stu1;
        stus[1]=stu2;
    }
    @Override
        //查询id是否存在
    public int  getIndex(String id){
        int index= -1;
        for (int i = 0; i <stus.length ; i++) {
            Student stu = stus[i];
            if(stu!=null && stu.getId().equals(id)){
                index=i;
                break;
            }
        }
        return index;

    }
    //添加学生
    public boolean addStudent(Student stu) {
        //添加学生到数组中
        int index = -1;//假设数组已经存满
        for (int i = 0; i <stus.length ; i++) {
            Student student = stus[i];
            if (student==null) {//如果数组没装满
                index = i;//用index记录当前下标
                break;
            }
        }
        if (index==-1){//装满了
            return  false;
        }else {//没装满,并且将学生对象添加到数组
            stus[index] = stu;
            return true;
        }

    }
    public Student[] findAllStudent() {
        return stus;
    }

    public void deleteStudentById(String id) {
        int index = getIndex(id);//查询被删除id索引的位置在不在
        stus[index]=null;//将索引位置使用null覆盖
    }

    public void updateStudent(String updateId, Student newStu) {
        int index = getIndex(updateId);
        stus[index] = newStu;
    }
}
